package com.plate.modules.system.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.plate.common.core.result.Result;
import com.plate.modules.system.domain.bo.SysRoleQueryBO;
import com.plate.modules.system.domain.bo.SysRoleSaveBO;
import com.plate.modules.system.domain.bo.SysUserRoleBO;
import com.plate.modules.system.domain.po.SysRole;
import com.plate.modules.system.domain.po.SysUserRole;
import com.plate.modules.system.service.ISysRoleService;
import com.plate.modules.system.service.ISysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName SysRoleController
 * @Description TODO
 * @Author jiaxd
 * @Date 2023年10月23日 22:16
 * @Version 1.0
 */

@RestController
@RequestMapping("/sys/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysUserRoleService sysUserRoleService;

    @PostMapping("/list")
    public Result list(@RequestBody SysRoleQueryBO queryBO) {
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        if (!StrUtil.isBlankIfStr(queryBO.getRoleName())) {
            wrapper.like(SysRole::getRoleName, queryBO.getRoleName());
        }
        if (!StrUtil.isBlankIfStr(queryBO.getRoleCode())) {
            wrapper.eq(SysRole::getRoleCode, queryBO.getRoleCode());
        }
        if (!StrUtil.isBlankIfStr(queryBO.getStatus())) {
            wrapper.eq(SysRole::getStatus, queryBO.getStatus());
        }
        Page<SysRole> page = new Page<>(queryBO.getPage(), queryBO.getSize());
        Page<SysRole> rolePage = sysRoleService.page(page, wrapper);
        return Result.SUCCESS(rolePage);
    }

    @PostMapping("/update")
    public Result updateUser(@RequestBody SysRoleSaveBO saveBO) {
        if (StrUtil.isBlankIfStr(saveBO.getId())) {
            // 新增
            SysRole sysRole = new SysRole();
            sysRole.setRoleName(saveBO.getRoleName());
            sysRole.setRoleCode(saveBO.getRoleCode());
            sysRole.setSort(saveBO.getSort());
            sysRole.setStatus(saveBO.getStatus());
            sysRole.setRemark(saveBO.getRemark());
            sysRole.setDataScope(saveBO.getDataScope());
            int save = sysRoleService.isSave(sysRole);
        } else {
            // 修改
            SysRole sysRole = sysRoleService.getById(saveBO.getId());
            sysRole.setRoleName(saveBO.getRoleName());
            sysRole.setSort(saveBO.getSort());
            sysRole.setStatus(saveBO.getStatus());
            sysRole.setRemark(saveBO.getRemark());
            sysRole.setDataScope(saveBO.getDataScope());
            sysRoleService.isUpdate(sysRole);
        }
        return Result.SUCCESS();
    }

    @DeleteMapping("delete/{id}")
    public Result deleteSysRole(@PathVariable("id") Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        if (ObjectUtil.isEmpty(sysRole)) {
            return Result.FAIL("");
        }
        boolean b = sysRoleService.removeById(id);
        return Result.SUCCESS(b);
    }

//    @PostMapping("bindUserRole")
//    public Result bindUserRole(@RequestBody SysUserRoleBO saveBO) {
//        List<SysUserRole> collect = saveBO.getUserIdList().stream().map(x -> {
//            SysUserRole sysUserRole = new SysUserRole();
//            sysUserRole.setRoleId(saveBO.getRoleId());
//            sysUserRole.setUserId(Long.parseLong(x));
//            return sysUserRole;
//        }).collect(Collectors.toList());
//        boolean b = sysUserRoleService.saveBatch(collect);
//        return Result.SUCCESS(b);
//    }

//    @PostMapping("cancelUserRole")
//    public Result cancelUserRole(@RequestBody SysUserRoleBO saveBO) {
//        List<SysUserRole> collect = saveBO.getUserIdList().stream().map(x -> {
//            SysUserRole sysUserRole = new SysUserRole();
//            sysUserRole.setRoleId(saveBO.getRoleId());
//            sysUserRole.setUserId(Long.parseLong(x));
//            return sysUserRole;
//        }).collect(Collectors.toList());
//        sysUserRoleService.removeBatch(collect);
//        return Result.SUCCESS();
//    }

}
